package com.zhenyi.xiaohuoche.admin;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cjw.model.DBMap;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.SqlPara;
import com.xhc.common.model.XhcTicket;
import com.zhenyi.xhc.core.XHCAuthInterceptor;
import com.zhenyi.xhc.core.XHCController;

@Before(XHCAuthInterceptor.class)
public class AdminTicketController extends XHCController {

	/**
	 * 例如指定日的票务(ticket)信息,列出时间段,标题,购票数量
	 */
	@Override
	public void list() {
		String startDate = getPara("startDate");
		String endDate = getPara("endDate");
		SqlPara sqlPara = Db.getSqlPara("TicketList", startDate, endDate);
		List<XhcTicket> items = XhcTicket.dao.find(sqlPara);
		for (XhcTicket xhcTicket : items) {
			xhcTicket.setDefaultInfo();
		}
		renderResult(items);
	}

	/**
	 * 票务日期是否在选中星期数组中
	 * 
	 * @param week
	 * @param item
	 * @return
	 */
	public Boolean DayOfWeek(XhcTicket item, String weeks) {
		Date date = item.getTime();
		Integer day = date.getDay();
		String itemWeek = String.valueOf(day);
		String[] weekArr = weeks.split(";");
		Boolean flag = false;
		for (String week : weekArr) {
			if (week.equals(itemWeek)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 * 票务时间是否在选中时间范围内
	 * 
	 * @param item
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public Boolean TimeOfRange(XhcTicket item, String startTimeStr, String endTimeStr) {
		SimpleDateFormat dateFm = new SimpleDateFormat("HH");
		String hoursStr = dateFm.format(item.getTime());
		int hours = Integer.parseInt(hoursStr);
		String startTimeFmt = startTimeStr.split(":")[0];
		String endTimeStrFmt = endTimeStr.split(":")[0];
		int startTime = Integer.parseInt(startTimeFmt);
		int endTime = Integer.parseInt(endTimeStrFmt);
		Boolean flag = false;
		if (hours >= startTime && hours < endTime) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 重置票务信息
	 * 
	 * @param items
	 * @return
	 */
	public Boolean reset(ArrayList<XhcTicket> items) {
		Boolean flag = Db.tx(new IAtom() {
			public boolean run() throws SQLException {
				for (XhcTicket item : items) {
					item.setTitle1(null);
					item.setTitle2(null);
					item.setTitle3(null);
					item.setTitle4(null);
					item.setTitle5(null);
					item.setPrice1(null);
					item.setPrice2(null);
					item.setPrice3(null);
					item.setPrice4(null);
					item.setPrice5(null);
					item.setTips(null);
					item.setLimit(null);
					if (!item.update()) {
						return false;
					}
				}
				return true;
			}
		});
		return flag;
	}

	/**
	 * 获取默认票务默认值
	 */
	public void getDefaultInfo() {
		XhcTicket xhcTicket = new XhcTicket();
		xhcTicket.setDefaultInfo();
		DBMap map = new DBMap();
		map.put("limit", xhcTicket.getLimit());
		map.put("price1", xhcTicket.getPrice1());
		map.put("price2", xhcTicket.getPrice2());
		map.put("price3", xhcTicket.getPrice3());
		map.put("price4", xhcTicket.getPrice4());
		map.put("price5", xhcTicket.getPrice5());
		map.put("title1", xhcTicket.getTitle1());
		map.put("title2", xhcTicket.getTitle2());
		map.put("title3", xhcTicket.getTitle3());
		map.put("title4", xhcTicket.getTitle4());
		map.put("title5", xhcTicket.getTitle5());
		renderResult(map);
	}

	/**
	 * 修改指定时间段票价 如2018-09-09至2018-10-10 星期一到五,10:00-12:00的儿童票,成人票的票价,购票上线/下线
	 */
	@Override
	public void submit() {
		String startDate = getPara("startDate");
		String endDate = getPara("endDate");
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		String week = getPara("week", "1;2;3;4;5;6;7");
		String title1 = getPara("title1", null);
		String title2 = getPara("title2", null);
		String title3 = getPara("title3", null);
		String title4 = getPara("title4", null);
		String title5 = getPara("title5", null);
		BigDecimal price1 = new BigDecimal(getPara("price1", "0"));
		BigDecimal price2 = new BigDecimal(getPara("price2", "0"));
		BigDecimal price3 = new BigDecimal(getPara("price3", "0"));
		BigDecimal price4 = new BigDecimal(getPara("price4", "0"));
		BigDecimal price5 = new BigDecimal(getPara("price5", "0"));
		String tips = getPara("tips", "");
		Integer limit = getParaToInt("limit", null);
		Integer[] weekCount = { 0, 0, 0, 0, 0, 0, 0 };
		String[] weekArr = week.split(";");
		for (String weekStr : weekArr) {
			int weekInt = Integer.parseInt(weekStr);
			weekCount[weekInt - 1] = 1;
		}
		if (StrKit.isBlank(startDate) && StrKit.isBlank(endDate)) {
			renderMsg("日期范围不能为空");
			return;
		}
		if (StrKit.isBlank(startTime) && StrKit.isBlank(endTime)) {
			renderMsg("时间范围不能为空");
			return;
		}
		SqlPara sqlPara = Db.getSqlPara("FindRangeTickes", startDate, endDate);
		List<XhcTicket> items = XhcTicket.dao.find(sqlPara);
		ArrayList<XhcTicket> temp = new ArrayList<>();

		for (XhcTicket item : items) {
			Integer day = item.getTime().getDay();
			if (TimeOfRange(item, startTime, endTime) && weekCount[day - 1] == 1) {
				temp.add(item);
			}
		}

		Boolean flag = Db.tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				for (XhcTicket item : temp) {
					item.setTitle1(title1);
					item.setTitle2(title2);
					item.setTitle3(title3);
					item.setTitle4(title4);
					item.setTitle5(title5);
					item.setPrice1(price1);
					item.setPrice2(price2);
					item.setPrice3(price3);
					item.setPrice4(price4);
					item.setPrice5(price5);
					item.setTips(tips);
					item.setLimit(limit);
					if (!item.update()) {
						return false;
					}
				}
				return true;
			}
		});
		renderFlag(flag);
	}

	/**
	 * 设置营业时间,结束营业时间,基本票价(成人,儿童,套票)
	 */
	@Override
	public void update() {
		super.update();
	}

	/**
	 * 更新购票协议,上传txt文本
	 */
	public void updateAgreement() {
	}

	public void refund() {
		Long id = getParaToLong("id");
		orderUtils.refundTicket(this, id, true);
	}

}
